package com.tjetc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tjetc.common.Result;
import com.tjetc.entity.*;
import com.tjetc.mapper.UserMapper;
import com.tjetc.service.IActivityService;
import com.tjetc.service.IUserActivityService;
import com.tjetc.service.IUserMessageService;
import com.tjetc.service.IUserTribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


@RestController
@RequestMapping("/userActivity")
public class UserActivityController {
    @Autowired
    private IUserActivityService userActivityService;

    @Autowired
    private IUserTribeService userTribeService;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IUserMessageService userMessageService;

    @RequestMapping("/applySign")
    public Result<?> applySign(Integer uid, Integer aid) {
        //判断是否在在报名时间
        Activity activity = activityService.getById(aid);
        System.out.println(activity.getUpBeginTime());

        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        String dateTimeStr = activity.getUpBeginTime().format(formatter);
        //报名开始时间

        LocalDateTime givenTime = LocalDateTime.parse(dateTimeStr, formatter);
        //现在时间

        LocalDateTime currentTime = LocalDateTime.now();


        String dateTimeStrend = activity.getUpEndTime().format(formatter);
        //报名结束时间

        LocalDateTime givenTime1 = LocalDateTime.parse(dateTimeStrend, formatter);

        if (currentTime.isBefore(givenTime)) {
            return Result.fail("此活动未开始报名");
        } else if (currentTime.isAfter(givenTime1)) {
            return Result.fail("报名时间已过");
        } else {
            //判断是否可以参加
            Integer apply = activity.getApply();
            if (apply == 2) {
                //此部落人员
                QueryWrapper<UserTribe> qw = new QueryWrapper<UserTribe>();
                qw.eq("user_id", uid);

                qw.eq("tribe_id", activity.getTribeId());
                UserTribe userTribe = userTribeService.getOne(qw);
                if (userTribe == null) {
                    return Result.fail("无权限报名");
                } else {
                    //是否已经报名
                    QueryWrapper<UserActivity> qw1 = new QueryWrapper<UserActivity>();
                    qw1.eq("user_id", uid);

                    qw1.eq("aid", aid);
                    UserActivity userActivity = userActivityService.getOne(qw1);
                    if (userActivity != null) {
                        return Result.fail("请勿重复报名");
                    } else {
                        UserActivity userActivity1 = new UserActivity();
                        userActivity1.setUserId(uid);
                        userActivity1.setAid(aid);
                        userActivity1.setState(0);
                        boolean save = userActivityService.save(userActivity1);
                        if (save) {
                            return Result.success();
                        } else {
                            return Result.fail();
                        }

                    }
                }

            }
            else{
                //是否已经报名
                QueryWrapper<UserActivity> qw1 = new QueryWrapper<UserActivity>();
                qw1.eq("user_id", uid);

                qw1.eq("aid", aid);
                UserActivity userActivity = userActivityService.getOne(qw1);
                if (userActivity != null) {
                    return Result.fail("请勿重复报名");
                } else {
                    UserActivity userActivity1 = new UserActivity();
                    userActivity1.setUserId(uid);
                    userActivity1.setAid(aid);
                    userActivity1.setState(0);
                    boolean save = userActivityService.save(userActivity1);
                    if (save) {
                        return Result.success();
                    } else {
                        return Result.fail();
                    }

                }
            }

            }


        }

        @RequestMapping("/list")
    public PageInfo<UserActivity> list(@RequestParam(defaultValue = "1") Integer curPage,
                                       @RequestParam(defaultValue = "10") Integer pageSize,
                                       @RequestParam(defaultValue = "10") Integer userActivityId

                                    ){
            return userActivityService.lists(curPage,pageSize,userActivityId);

        }

        @RequestMapping("/check")
    public Result<?>   check  (Integer id){
            UserActivity userActivity =userActivityService.getById(id);
            userActivity.setState(1);
            boolean b = userActivityService.updateById(userActivity);
            //发通知
            Activity activity=activityService.getById(userActivity.getAid());
            UserMessage userMessage = new UserMessage();
            userMessage.setActivityId(userActivity.getAid());
            userMessage.setUserId(userActivity.getUserId());
            userMessage.setContent("您报名的活动"+activity.getName()+"已经通过审核，请按时完成活动任务");
            userMessageService.save(userMessage);

            if (b){
                return Result.success();
            }else{
                return Result.fail();
            }

        }







}
